package com.yj.ccs.abs.web.assetData.task;

import com.yj.ccs.abs.web.assetData.entity.Periods;
import com.yj.ccs.abs.web.assetData.mapper.PeriodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/4/9
 * Author     : Unbelievable9
 **/
@Service
public class PeriodsTask {

    @Autowired
    PeriodsMapper periodsMapper;

    @Scheduled(cron = "59 59 23 28-31 * ?")
    public void createPeriodsScheduleTask() {
        // TODO: 后续需要修改

        Periods latestPeriods = periodsMapper.getLastestPeriods();
        Periods newPeriods = new Periods();
        newPeriods.setStart_date(LocalDate.now().plusDays(1));
        newPeriods.setEnd_date(LocalDate.now().plusMonths(1).minusDays(1));
        newPeriods.setCreate_time(LocalDateTime.now());

        if (latestPeriods ==null) {
            newPeriods.setPeriod(1L);
        } else {
            newPeriods.setPeriod(latestPeriods.getPeriod() + 1);
        }

        periodsMapper.create(newPeriods);
    }
}
